#!/usr/bin/python

import pygame
import Effect


# An effect which grows the target entity for a period of time
class GrowEffect(Effect.Effect):

    # Obvious
    def __init__(self, targetEntity, growth, durationms):
        super(GrowEffect, self).__init__("sizechange", targetEntity)
        self.m_growth = growth
        self.m_durationms = durationms
        # Not yet in effect
        self.m_inEffect = False
        self.m_expireTime = 0

    # Called each frame to allow effect to make updates
    def frameUpdate(self, elapsedms, absolutems):
        if not(self.m_inEffect):
            # Grow target
            print "Growing..."
            self.applyEffect()
            self.m_expireTime = absolutems + self.m_durationms
        else:
            if self.m_expireTime <= absolutems:
                self.unapplyEffect()
                # Remove from target
                self.m_targetEntity.removeEffect(self.m_name)

        # Call parent
        super(GrowEffect, self).frameUpdate(elapsedms, absolutems)

    # Apply this effect to target
    def applyEffect(self):
        # Don't double apply
        if self.m_inEffect:
            return
        (w,h)=self.m_targetEntity.getSize()

        # Make sure we don't shrink to nothing
        while self.m_growth + w < 1:
            self.m_growth = self.m_growth + 1
        while self.m_growth + h < 1:
            self.m_growth = self.m_growth + 1

        # Adjust size
        self.m_targetEntity.setSize(w+self.m_growth, h+self.m_growth)
        self.m_inEffect=True
        
    # Remove this effect from target
    def unapplyEffect(self):
        # Don't double remove
        if not(self.m_inEffect):
            return;
        (w,h)=self.m_targetEntity.getSize()
        self.m_targetEntity.setSize(w-self.m_growth, h-self.m_growth)
        self.m_inEffect=False




